package cn.laixueit.controller;

import cn.laixueit.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView();
        view.setViewName("index");
        view.addObject("msg","springmvc");
        return view;
    }

    @RequestMapping("/index2")
    public ModelAndView index2(){
        ModelAndView view = new ModelAndView();
        view.setViewName("index");
        view.addObject("msg","张三");
        return view;
    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public ModelAndView findById(Integer id){
        ModelAndView view = new ModelAndView();
        view.setViewName("index");
        view.addObject("msg","id");
        view.addObject("id",id);
        return view;
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public ModelAndView save(String name,Integer age){
        System.out.println("name:"+name+",age:"+age);
        return new ModelAndView();
    }

    @RequestMapping(method = RequestMethod.POST,value = {"saveStudent","addStudent"})
    public void saveStudent(Student student){
        System.out.println(student+"保存到service-》dao");
    }

    @RequestMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/a.jsp").forward(request,response);
//        response.sendRedirect();
    }

    @RequestMapping("/returnview")
    public String returnview(Model model){
        model.addAttribute("msg","nihao");
        model.addAttribute("id","110");
        return "redirect:/index.jsp";
    }

}
